package com.drinks.yiniang.adapter.photo;

import android.content.Context;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.common.camera.album.entity.GoodsTagPoints;
import com.drinks.yiniang.entity.photo.Photos;
import com.drinks.yiniang.ui.widget.layout.RandomDragTagLayout;
import com.drinks.yiniang.ui.widget.view.RandomDragTagView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预览图片adapter
 * @author han
 * @date 2019/8/30
 */
public class PreviewPhotosAdapter extends BaseQuickRCVAdapter<Photos, BaseViewHolder> {

    private Map<Photos, List<GoodsTagPoints>> mTagList;
    private RandomDragTagLayout.OnClickPointsAddTagListener onAddTagClickLiesener;

    private StringBuilder mBuilder;

    public PreviewPhotosAdapter(Context mContext, @Nullable List<Photos> data) {
        super(R.layout.item_preview_photos, data);

        mTagList = new HashMap<>();
    }

    @Override
    protected void convert(BaseViewHolder helper, Photos item) {
        helper.setIsRecyclable(false);

        GlideUtils.load(mContext, (ImageView) helper.getView(R.id.iv_preview_photo), item.path);

        RandomDragTagLayout fl_photos_drag_tag = helper.getView(R.id.fl_photos_drag_tag);

        fl_photos_drag_tag.removeAllViews();

        if(!CommonUtils.isEmptyList(mTagList.get(item))) {
            for(GoodsTagPoints tagData : mTagList.get(item)) {
                mBuilder = new StringBuilder(StringUtils.getNoNullString(tagData.getGoodsName()));
                mBuilder.append(" ¥");
                mBuilder.append(StringUtils.getNoNullString(tagData.getGoodsPrice()));

                tagData.setTagView(fl_photos_drag_tag.addTagView(item, mBuilder.toString(), tagData.getX(), tagData.getY(), tagData.isShowLeft()));
            }
        }

        fl_photos_drag_tag.setOnClickPointsAddTagListener(new RandomDragTagLayout.OnClickPointsAddTagListener() {
            @Override
            public void onTagAddClick(Photos photos, float x, float y) {
                if(onAddTagClickLiesener != null) {
                    onAddTagClickLiesener.onTagAddClick(photos, x, y);
                }
            }

            @Override
            public void onTagDragChange(RandomDragTagView tagView, Photos photos, float x, float y, boolean isLeft) {
                if(onAddTagClickLiesener != null) {
                    onAddTagClickLiesener.onTagDragChange(tagView, photos, x, y, isLeft);
                }
            }
        });
    }

    public Map<Photos, List<GoodsTagPoints>> getTagList() {
        return mTagList;
    }

    public void setOnAddTagClickLiesener(RandomDragTagLayout.OnClickPointsAddTagListener onAddTagClickLiesener) {
        this.onAddTagClickLiesener = onAddTagClickLiesener;
    }
}
